;   p2_5.s
  ;   Toggling a single LED in Assembly	
  ;
  ;   This program turns on the red LED and toggles
  ;   the blue LED 0.5 sec on and 0.5 sec off.
  ;
        THUMB

        AREA    |.text|, CODE, READONLY, ALIGN=2
        ALIGN          
        EXPORT  __main

__main
        ; enable clock to GPIOF at clock gating register
        LDR     R0, =0x400FE608	; RCGC reg. addr.
        LDR     R1, [R0]
        ORR     R1, #0x20
        STR     R1, [R0]

        ; set PORTF pin3-1 as output pins
        LDR     R0, =0x40025400	
                ;DIR register address
        MOV     R1, #0x0E
        STR     R1, [R0]

        ; set PORTF pin3-1 as digital pins
        LDR     R0, =0x4002551C	
                ; Digital Enable reg. addr.
        MOV     R1, #0x0E
        STR     R1, [R0]
        
        ; turn on red LED only and leave it on
        LDR     R0, =0x400253FC	
                ;PORTF reg. address
        MOV     R1, #2
        STR     R1, [R0]        

loop
        ; write PORTF to turn on blue LED
        LDR     R0, =0x400253FC
        LDR     R1, [R0]
        ORR     R1, #4
        STR     R1, [R0]
        ; delay a little while
        LDR     R0, =500
        BL      delayMs
        
        ; write PORTF to turn off blue LED
        LDR     R0, =0x400253FC
        LDR     R1, [R0]
        AND     R1, #~4
        STR     R1, [R0]
        ; delay a little while
        LDR     R0, =500
        BL      delayMs
        
        ; repeat the loop
        B       loop

  ; This subroutine performs a delay of n ms. 
  ; n is the value in R0
delayMs 
        MOVS    R3, R0
        BNE     L1           ; if n = 0, return
        BX      LR           ; return
L1      LDR     R4, =5336    
        ; do inner loop 5336 times (for 16 MHz CPU clock)
L2      SUBS    R4, R4, #1   ; inner loop
        BNE     L2
        SUBS    R3, R3, #1   ; do outer loop n times
        BNE     L1
        BX      LR           ; return

  ; This subroutine grants access to 
  ; floating point coprocessor.
  ; It is called by the startup code.
        EXPORT  SystemInit
SystemInit
        LDR     R0, =0xE000ED88           
                ; Enable CP10,CP11
        LDR     R1,[R0]
        ORR     R1,R1,#(0xF << 20)
        STR     R1,[R0]
        BX      LR

  ; This variable is required for 
  ; the startup code though not used.
        EXPORT  __use_two_region_memory
__use_two_region_memory EQU 1

        ALIGN
        END